package trunk.frontend;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class RadioButtonOptions extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JButton jbSubmit;
	private JButton jbCancel;
	private JTextField jtfsize = new JTextField(12);
	private JTextField[] jtf = new JTextField[7];

	private WFBFrame parent;

	public RadioButtonOptions(WFBFrame myParent) {
		super("Text Field Creator");
		setSize(new Dimension(400, 300));
		JLabel jlTitle = new JLabel("Choose the Number");
		JLabel jlTitle2 = new JLabel("of Radio Buttons (1-7): ");
		JLabel jlWhatContents = new JLabel("Fill in Contents Here");
		
		jbSubmit = new JButton("Submit");
		jbSubmit.addActionListener(this);
		jbCancel= new JButton("Cancel");
		jbCancel.addActionListener(this);
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		
		gbc.anchor = GridBagConstraints.NORTH;
		gbc.gridx = GridBagConstraints.RELATIVE;
		gbc.gridy = 0;
		
		getContentPane().setLayout(gbl);
		getContentPane().add(jlTitle, gbc);
		gbc.gridy = 1;
		getContentPane().add(jlTitle2, gbc);
		getContentPane().add(jtfsize, gbc);
		gbc.gridy = 2;
		getContentPane().add(jlWhatContents, gbc);
		for (int i = 0; i < 7; i++) {
			jtf[i] = new JTextField(12);
			gbc.gridy = i + 3;
			getContentPane().add(jtf[i], gbc);
		}
		
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridy = 10;
		
		getContentPane().add(jbSubmit, gbc);
		getContentPane().add(jbCancel, gbc);
		
		parent = myParent;
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == jbSubmit) {
			int likertTotal = 0;
			try {
				likertTotal = Integer.parseInt(jtfsize.getText());
				if (likertTotal < 1 || likertTotal > 7) {
					JOptionPane.showMessageDialog(null, "You must type in a number 1-7.");
				} else {
					String toAdd = "4#:@Radio Buttons";
					String labels = new String();
					String values = "#:@#:%";
					for (int i = 0; i < likertTotal; i++) {
						labels = labels.concat("#:@" + jtf[i].getText());
						values = values.concat("#:@" + i);
					}
					parent.addToForm(toAdd + labels + values);
					dispose();
				}
			} catch (NumberFormatException nfe) {
				JOptionPane.showMessageDialog(null, "You must type in a number 1-7.");
			}
			//setVisible(false);
		}
		else if (ae.getSource() == jbCancel) {
			dispose();
			//setVisible(false);
		}
	}
	
}
